﻿using System;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYFitting : HYMEPObject, IHYXmlSerial
	{
		public double Coefficient
		{
			get
			{
				if (!Utility.IsZero(this.m_UserCoefficient, 1E-05))
				{
					return this.m_UserCoefficient;
				}
				return this.m_Coefficient;
			}
			set
			{
				this.m_Coefficient = value;
			}
		}

		public IHYSpecification InPort
		{
			get
			{
				return this.m_InPort;
			}
			set
			{
				this.m_InPort = value;
			}
		}

		public IHYSpecification OutPort
		{
			get
			{
				return this.m_OutPort;
			}
			set
			{
				this.m_OutPort = value;
			}
		}

		public string CoefficientShowString { get; set; }

		public HYFitting()
		{
			base.Name = "管件";
			this.m_HostPipeline = null;
			this.m_Coefficient = 0.0;
			this.m_UserCoefficient = 0.0;
			this.CoefficientShowString = "管件";
		}

		public override void SetNext(HYMEPObject nextObj)
		{
			base.SetNext(nextObj);
			if (nextObj is HYPipeline)
			{
				this.m_HostPipeline = (nextObj as HYPipeline);
			}
		}

		public virtual HYPipeline GetHostPipeline()
		{
			if (this.m_HostPipeline != null)
			{
				return this.m_HostPipeline;
			}
			HYMEPObject next = base.Next;
			while (next != null && !(next is HYPipeline))
			{
				next = next.Next;
			}
			if (next == null)
			{
				return null;
			}
			this.m_HostPipeline = (next as HYPipeline);
			return this.m_HostPipeline;
		}

		public virtual void CalCoefficient()
		{
		}

		public override void RebuildIndex(ref int index)
		{
			if (this.m_NextObject != null)
			{
				this.m_NextObject.RebuildIndex(ref index);
			}
		}

		public override void Cal(HYSystemOption option)
		{
			this.CalCoefficient();
			HYPipeline hostPipeline = this.GetHostPipeline();
			if (hostPipeline != null)
			{
				hostPipeline.AddAttachedObject(this, this.CoefficientShowString, this.Coefficient, false, 0);
			}
		}

		public virtual void SetCoeffient(double coef, int PartIndex = 0)
		{
			this.m_UserCoefficient = coef;
		}

		public override void ReadXml(XElement xe)
		{
			base.ReadXml(xe);
			this.m_Coefficient = xe.GetDoubleAttribute("Coefficient", 0.0);
			this.m_UserCoefficient = xe.GetDoubleAttribute("UserCoefficient", 0.0);
			if (this.m_InPort != null)
			{
				XElement xe2 = xe.ChildElement("In");
				this.m_InPort.ReadXml(xe2);
			}
			if (this.m_OutPort != null)
			{
				XElement xe3 = xe.ChildElement("Out");
				this.m_OutPort.ReadXml(xe3);
			}
		}

		public override void WriteXml(XElement xe)
		{
			base.WriteXml(xe);
			xe.SetAttributeValue("Coefficient", this.Coefficient);
			xe.SetAttributeValue("UserCoefficient", this.m_UserCoefficient);
			if (this.m_InPort != null)
			{
				XElement xe2 = xe.ChildElement("In");
				this.m_InPort.WriteXml(xe2);
			}
			if (this.m_OutPort != null)
			{
				XElement xe3 = xe.ChildElement("Out");
				this.m_OutPort.WriteXml(xe3);
			}
		}

		protected const string LeftBraket = "【";

		protected const string RightBraket = "】";

		protected IHYSpecification m_InPort;

		protected IHYSpecification m_OutPort;

		protected double m_Coefficient;

		protected double m_UserCoefficient;

		protected HYPipeline m_HostPipeline;
	}
}
